#include <string>
#include "userIncludes.hh"
#include <sys/stat.h> //to check the existence of a file
#include <online-certifier/util/typeDefines.hh>
#include <sstream>
using namespace std;

/*

userModularFunctionBase* userModularFunctionBase::initializeByName(string name){

    userModularFunctionBase* tmp;

    if(name == "determinant")
        tmp = new determinant;
//    else if(name == "testNumber")
  //      tmp = new testNumber;
    else if(name == "fibo")
        tmp = new fibo;
        
    else
        throw "Unsupported function call exception";

    tmp->setName(name);
    return tmp;

}*/

userModularFunctionBase* userModularFunctionBase::initializeByName(string name, vector<string> args){

    userModularFunctionBase* tmp;

    if(name == "determinant")
        tmp = new determinant(args);
//    else if(name == "testNumber")
  //      tmp = new testNumber;
    else if(name == "fibo")
        tmp = new fibo(args);
        
    else
        throw "Unsupported function call exception";

    tmp->setName(name);
    return tmp;

}

void userModularFunctionBase::setFile(int index){
    //check the existence of the file
    char* stringToken;

    string tmp = arguments[index];
    char fullName[(arguments[index]).size()];
    strcpy(fullName, (arguments[index]).c_str());


    struct stat buffer ;
    
    if ( stat( (arguments[index]).c_str(), &buffer ) ) throw "file not exist exception" ;


    stringToken = strtok(fullName , "/");

    string file;
    while(stringToken != NULL){
        file = stringToken;
        stringToken = strtok(NULL,"/");
    }


    
    arguments[index] = file;//just store the file name without directory 
    files.push_back(make_pair(index,tmp)); //full path file name

}
/*
void userModularFunctionBase::updateFileNames(string prefix){

    for(size_t i=0; i < files.size(); i++){

        stringstream name;
        name << prefix << i;
        files[i]= name.str();
    }


}
*/
std::ostream& operator<< (std::ostream& os,  userModularFunctionBase& b){
    b.write_to_ostream(os);
    return os;
}
userModularFunctionBase::~userModularFunctionBase(){

}
